/*
    Bandleader
    Copyright (c) 2006 by Grant Yoshida

    Licensed under the GPL version 2 or later,
    please see COPYING for details.
*/

#include "MainWindow.h"
//
#include "ChordView.h"
#include "Document.h"
//
#include <QtGui/QMenuBar>
#include <QtGui/QMenu>
#include <QtGui/QToolBar>
//
#include <QtGui/QMessageBox>
//
#include <QtGui/QUndoGroup>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_chordView(0),
    m_undoGroup(0)
{
    DocumentPtr document(new Document());
    m_chordView = new ChordView(document);
    setCentralWidget(m_chordView);

    connect(document.get(), SIGNAL(nameChanged(const QString &)),
            this, SLOT(setTitleFrom(const QString &)));

    m_chordView->setFocus();
    resize(m_chordView->size());

    m_undoGroup = new QUndoGroup(this);
    m_undoGroup->addStack(m_chordView->undoStack());

    QMenuBar *menu = new QMenuBar();
    //
    QMenu *fileMenu = menu->addMenu("&File");
    fileMenu->addAction(QIcon("document-save.png"), "&Save", m_chordView, SLOT(save()));
    fileMenu->addAction(QIcon("document-save-as.png"), "Save &As...", m_chordView, SLOT(saveAs()));
    fileMenu->addSeparator();
    fileMenu->addAction("E&xit", this, SLOT(close()));
    //
    QMenu *editMenu = menu->addMenu("&Edit");
    QAction *undoAction = m_undoGroup->createUndoAction(editMenu, "&Undo");
    QAction *redoAction = m_undoGroup->createRedoAction(editMenu, "&Redo");
    undoAction->setIcon(QIcon("edit-undo.png"));
    redoAction->setIcon(QIcon("edit-redo.png"));
    editMenu->addAction(undoAction);
    editMenu->addAction(redoAction);
    editMenu->addSeparator();
    editMenu->addAction(QIcon("edit-cut.png"), "Cu&t", m_chordView, SLOT(cut()));
    editMenu->addAction(QIcon("edit-copy.png"), "&Copy", m_chordView, SLOT(copy()));
    editMenu->addAction(QIcon("edit-paste.png"), "&Paste", m_chordView, SLOT(paste()));
    editMenu->addAction(QIcon("edit-delete.png"), "&Delete", m_chordView, SLOT(deleteSelection()));
    //
    QMenu *helpMenu = menu->addMenu("&Help");
    helpMenu->addAction(QIcon("app-icon.png"), "About Bandleader", this, SLOT(about()));
    setMenuBar(menu);

    //
    QToolBar *mediaToolBar = new QToolBar("Media");
    QAction *playAction =
        mediaToolBar->addAction(QIcon("media-playback-start.png"), "Play",
                                m_chordView, SLOT(togglePlaying()));
    playAction->setCheckable(true);
    playAction->setShortcut(Qt::Key_Space);
    connect(m_chordView, SIGNAL(playingStateChanged(bool)),
            playAction, SLOT(setChecked(bool)));
    //
    mediaToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    mediaToolBar->setMovable(false);
    //
    addToolBar(mediaToolBar);

    setTitleFrom(QString());
    setWindowIcon(QIcon("app-icon.png"));
}

void MainWindow::setTitleFrom(const QString &documentName)
{
    if (documentName.isEmpty())
        setWindowTitle("Bandleader");
    else
        setWindowTitle("Bandleader - " + documentName);
}

void MainWindow::about()
{
    QMessageBox::about(this, "About Bandleader", "Bandleader 0.1");
}
